﻿/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Author:       François PIETTE @ Balteau-NDT
Creation:     Feb 11, 2015
Description:  Demo showing how to automate Balteau NDT control Balteau-NDT
              applications using a TCP/IP connection.
              This source is a class aimed at using INI files to save
              persistent data.
Version:      1.00
Legal issues: This software is Open Source and subject to "The MIT License (MIT)"
              Copyright (C) 2015 Balteau-NDT
              Rue Voie de Liège 12, 4681 Hermalle Ss Argenteau, Belgium
              http://www.balteau-ndt.com
 
              Permission is hereby granted, free of charge, to any person 
              obtaining a copy of this software and associated documentation
              files (the "Software"), to deal in the Software without 
              restriction, including without limitation the rights to use, 
              copy, modify, merge, publish, distribute, sublicense, and/or 
              sell copies of the Software, and to permit persons to whom 
              the Software is furnished to do so, subject to the following 
              conditions:

              The above copyright notice and this permission notice shall 
              be included in all copies or substantial portions of the Software.

              THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
              EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
              OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
              NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
              HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
              WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
              OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
              DEALINGS IN THE SOFTWARE.

History:


 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace Balteau
{
    public class IniFile
    {    
        public string path;

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section,
                 string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section,
                 string key, string def, StringBuilder retVal,
                 int size, string filePath);

        public IniFile(string INIPath)
        {
            path = INIPath;
        }

        public void WriteString(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, this.path);
        }
        
        public void WriteInteger(string Section, string Key, int Value)
        {
            WriteString(Section, Key, Value.ToString());
        }
        
        public string ReadString(string Section, string Key, string DefaultValue)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, DefaultValue, temp, 
                                            255, this.path);
            return temp.ToString();
        }

        public int ReadInteger(string Section, string Key, int DefaultValue)
        {
            return int.Parse(ReadString(Section, Key, DefaultValue.ToString()));
        }
    }
}
